/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "MusicChangerIphone.h"
#include "Manager.h"
#include <logs/Log.h>
#include <math/utl.h>
#include <noise/hard/macx/MusicTrack.h>
#include <noise/hard/openal/Creator.h>

namespace liba
{
namespace noise
{
	
static GBMusicTrack * song = 0;

MusicChanger::MusicChanger(double fadeout_time):
	global_volume( 1 ),
	current_volume( 1 ),
	fadeout_speed( 1.0 / fadeout_time )
{
	song = 0;
	
	hard::openal::Creator::register_music_changer(this);
}
	
MusicChanger::~MusicChanger()
{
	hard::openal::Creator::register_music_changer(0);

	if(song)
	{
		[song close];
		[song release];
	}
	song = 0;
}

void MusicChanger::change_music(const filesystem::String & next_music)
{
	next_music_name = next_music;
}

void MusicChanger::life_cycle(double delta_time)
{
	if( next_music_name == music_name )
	{
		current_volume += delta_time * fadeout_speed;
		current_volume = math::clamp( current_volume, 0.0, 1.0 );
		if(song)
		{
			[song setGain:current_volume*global_volume];
		}
	}
	else
	{
		current_volume -= delta_time * fadeout_speed;
		
		if(song)
		{
			[song setGain:current_volume*global_volume];
		}

		if( current_volume < 0 )
		{
			if(song)
			{
				[song close];
				[song release];
				song = 0;
			}
			
			if(hard::openal::Creator::can_play_music())
			{
			music_name = next_music_name;
			if( !music_name.empty() )
			{
				current_volume = 1;
//				channel->play( noise::Manager::create_datasource( music_name ) );

				NSString *songName = nil;
				{
                    std::string music_name_ext = (music_name + ".mp3").c_str();
					
                    std::string str_to_lower;
					const char * buff = music_name_ext.c_str();
					for( unsigned i = 0; i < music_name_ext.size(); ++i )
					{
						str_to_lower += tolower(buff[i]);
					}
					
					filesystem::String multilanguage_name = filesystem::String("%app_path%") + "/" + str_to_lower;
					NSString *filename = [[NSString alloc] initWithUTF8String:multilanguage_name.c_str()];

					NSFileManager *fileManager = [NSFileManager defaultManager];
					BOOL internalFileExist = [fileManager fileExistsAtPath:filename];
					
					if(internalFileExist)
						songName = [filename retain];
					
					[filename release];
				}
				
				if(!songName)
				{
                    std::string music_name_ext = (music_name + ".m4a").c_str();
					
                    std::string str_to_lower;
					const char * buff = music_name_ext.c_str();
					for( unsigned i = 0; i < music_name_ext.size(); ++i )
					{
						str_to_lower += tolower(buff[i]);
					}
					
					filesystem::String multilanguage_name = filesystem::String("%app_path%") + "/" + str_to_lower;
					NSString *filename = [[NSString alloc] initWithUTF8String:multilanguage_name.c_str()];
					
					NSFileManager *fileManager = [NSFileManager defaultManager];
					BOOL internalFileExist = [fileManager fileExistsAtPath:filename];
					
					if(internalFileExist)
						songName = [filename retain];
					
					[filename release];
				}
				

				song = [[GBMusicTrack alloc] initWithPath:songName];
				[song setRepeat:YES];
				[song play];
				[song setGain:current_volume*global_volume];
				[songName release]; songName = 0;
			}
		}
	}
}
}

void MusicChanger::change_volume(double volume)
{
	global_volume = volume;
}

void MusicChanger::app_goes_background()
{
	if(song)
	{
		[song close];
		[song release];
		song = 0;		
	}
}

void MusicChanger::app_goes_active()
{
	if(hard::openal::Creator::can_play_music())
	{
		next_music_name = music_name;
		music_name = "";
	}
}

} // namespace noise
} // namesapce liba
